package org.raymond.iworks.study.flowable;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OneTaskProcess {
    private static RuntimeService runtimeService;
    private static TaskService taskService;

    private static OneTaskProcess instance;

    private OneTaskProcess(RuntimeService runtimeService, TaskService taskService){
        this.runtimeService = runtimeService;
        this.taskService = taskService;
    }

    public static synchronized OneTaskProcess instance(RuntimeService runtimeService, TaskService taskService){
        if(instance==null){
            instance = new OneTaskProcess(runtimeService, taskService);
        }
        return instance;
    }

    public void startProcess(String assignee){
        Map<String, Object> person = new HashMap<>();
        person.put("name", assignee);
        Map<String, Object> variables = new HashMap<>();
        variables.put("person", person);
        runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);
    }

    public List<Task> getTasks(String assignee){
        return taskService.createTaskQuery().taskAssignee(assignee).list();
    }
}
